package wangxiaotao.controller;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import wangxiaotao.bean.vo.ShowVO;
import wangxiaotao.common.CommonResult;
import wangxiaotao.converter.ShowConverter;
import wangxiaotao.entity.Show;

@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/admin/template")
public class TemplateController extends BaseController{
    private final ShowConverter showConverter;

    @GetMapping("/list")
    public CommonResult getAllTemplate(){
        return CommonResult.success(showConverter.toShowVOList(showService.list()));
    }

    /**
     * 删除banner
     * @param id
     * @return
     */
    @GetMapping("banner/delete/{id}")
    public CommonResult deleteBannerById(@PathVariable Integer id){
        return showService.removeById(id)?CommonResult.success():CommonResult.fail();
    }

    /**
     * 上传banner
     * @param file
     * @return
     */
    @PostMapping("/banner/add")
    public CommonResult addBanner(@RequestParam("image") MultipartFile file){
        String bannerSrc = minioTool.saveBannerImage(file);
        Show show = new Show();
        show.setData(bannerSrc).setType("banner");
        return showService.save(show)?CommonResult.success():CommonResult.fail();
    }

    /**
     * 删除通告
     * @param id
     * @return
     */
    @GetMapping("/notice/delete/{id}")
    public CommonResult deleteNoticeById(@PathVariable Integer id){
        return showService.removeById(id)?CommonResult.success():CommonResult.fail();
    }

    /**
     * 添加通告
     * @param data
     * @return
     */
    @PostMapping("/notice/add")
    public CommonResult addNotice(@RequestBody String data){
        Show show = new Show();
        show.setData(data).setType("notice");
        return showService.save(show)?CommonResult.success(show):CommonResult.fail();
    }

    /**
     * 修改通告
     * @param showVO
     * @return
     */
    @PostMapping("/notice/update")
    public CommonResult updateNotice(@RequestBody ShowVO showVO){
        return showService.updateById(showConverter.toShow(showVO))?CommonResult.success():CommonResult.fail();
    }
}
